global class ONEN_OpportunityWebServices {
	
	public static String DefaultStage = ONEN_Constants.OPP_DEFAULT_STAGE;
	/*******************************************
	* Method to create an Opp. Called from a simple 
	* S-Control called by the new opp form.
	********************************************/ 
	WebService static Id createOppApex(Id contactId, Id recordType, Id accountId, Boolean typeIndividual,String primaryContactRole){ 
		//System.debug('in cloneOpp');
				
		Opportunity newOpp = new Opportunity();
		String OppNameSuffix;
		String OppNamePrefix;		
		
		//if typeIndividual = true, then make sure newOpp.AccountId is Null
		//newOpp.AccountId should only be set if typeIndividual = false 
		// new method to lookup individual acct id
		Id DefaultAccountId = ONEN_DefaultAccount.GetIndividualAccountId();		

		//Set close date
		newOpp.CloseDate = System.Today();
		//Set stage name
		newOpp.StageName = DefaultStage;
		
		//Set record type
		newOpp.RecordTypeID = recordType;
		
		//set the contact
		//fires CreateContactRoles trigger (after insert)
		newOpp.CR_Contact_Id__c = contactId; 
		newOpp.CR_Contact_Role__c=primaryContactRole;
		//Set Name
		//prefix
		if (typeIndividual) {
			Contact oppContact = [select FirstName, LastName from Contact where Id =:contactId];
			OppNamePrefix = oppContact.FirstName + ' ' + oppContact.LastName;
			newOpp.AccountId = NULL;
			newOpp.CR_HHRoles__c = true;			
		    
        } else {
        	Account oppAccount = [select Name from Account where Id =:accountId];
        	OppNamePrefix = oppAccount.Name;
        	if (accountId == DefaultAccountId){
				newOpp.AccountId = NULL;
			} else{
				newOpp.AccountId = accountId;
			}
			newOpp.CR_HHRoles__c = false;
        }
        
        //suffix
		ONEN_RecordTypes OppRecordTypes = new ONEN_RecordTypes('Opportunity');
		OppNameSuffix = OppRecordTypes.GetOppNamingSuffix(OppRecordTypes.GetRecordTypeName(newOpp.RecordTypeID));	
		
		//set it
		newOpp.Name = OppNamePrefix + ' ' + newOpp.CloseDate.year() + ' ' + OppNameSuffix;    
		
		try {
			//create new opp
			insert newOpp;
		} catch (System.DmlException e) {		
			for (Integer k = 0; k < e.getNumDml(); k++) {
			 System.debug(e.getDmlMessage(k)); 
			}
		}
		//this won't work if try fails
		return newOpp.id;
		
	} 
	

	
	/*******************************************
	* Method to clone an Opp. Called from a simple 
	* S-Control called by a button on an Opp.
	********************************************/ 
	WebService static Id cloneOpp(Id oppToClone, Integer Offset, String OffsetUnit){ 
		//System.debug('in cloneOpp');
		
		//find opp to clone
		Opportunity thisOpp = [select CloseDate,AccountId,Account.Name,Amount,StageName,RecordTypeId,Fund__c from Opportunity where Id =:oppToClone];
		
		// remember the account name
		string AccountName;
		if ( thisOpp.Account != null ) {AccountName = thisOpp.Account.Name;}
		
		// now remove the account property from the object (otherwise cloning won't work right)
		// since the cloned Opp won't have a valid id until inserted,
		// SFDC won't let it have a vaild relationship to Account
		thisOpp.Account = null;
		
		//clone opp
		Opportunity newOpp = thisOpp.clone(false);

		OpportunityContactRole primaryContactRole;
		OpportunityContactRole[] primaryContactRoles = [select ContactId, Contact.FirstName, Contact.LastName, Role, IsPrimary from OpportunityContactRole where OpportunityId = :oppToClone and IsPrimary=true];
		if ( primaryContactRoles.size() > 0 ) {
			primaryContactRole = primaryContactRoles[0];
			
			//MS: moved here from further down - skip if no CR's exist
			//MP: Set contact id & role to invoke trigger NOTE: find and put name of said triggers here in comments
			newOpp.CR_Contact_Id__c=primaryContactRole.ContactId;
			newOpp.CR_Contact_Role__c=primaryContactRole.Role;						
			
		}
		 
		//TODO: replace stubbed code with actual
		String NewOppName;
		String NewOppPrefix;
		String NewOppSuffix;
		Date NewCloseDate;
		
		Id DefaultAccountId = ONEN_DefaultAccount.GetIndividualAccountId();
		//System.debug('primaryContactRole.Role = ' + primaryContactRole.Role);
		
		if(AccountName==null||newOpp.AccountId==DefaultAccountId) {
		//if(primaryContactRole.Role=='Individual Donor') {
			NewOppPrefix = primaryContactRole.Contact.FirstName + ' ' + primaryContactRole.Contact.LastName;
		} else {
			NewOppPrefix = AccountName;
		}
		
		//MP: Get record type to be included in default new name
		ONEN_RecordTypes OppRecordTypes = new ONEN_RecordTypes('Opportunity');
		NewOppSuffix = OppRecordTypes.GetOppNamingSuffix(OppRecordTypes.GetRecordTypeName(newOpp.RecordTypeId));	
		
		//MP: Assume this is cloning for one year out
		Integer NewYear;
		Integer NewMonth; 
		String newCloseDateName;
		//set the new dates and names for the new opp based on offset and offsetunit
		if (OffsetUnit=='Year') {			
			NewYear = newOpp.CloseDate.year()+ Offset;
			newCloseDateName = String.valueOf(NewYear);
			NewCloseDate = newOpp.CloseDate.addYears(Offset);
		} else if (OffsetUnit=='Month') {
			//set the date and date name for a month offsetunit			
			NewCloseDate = newOpp.CloseDate.addMonths(Offset);
			NewYear = NewCloseDate.year();
			NewMonth = NewCloseDate.Month();
			newCloseDateName = String.valueOf(NewMonth) + '/'+ String.valueOf(NewYear);
		}
		
		//MP: increase close date by one year
		newOpp.CloseDate = NewCloseDate;
		
		//MP: Name it with just the year
		newOpp.Name=NewOppPrefix + ' ' + newCloseDateName + ' ' + NewOppSuffix;
		
		//MP: assign correct stage NOTE: do we assume prospecting?
		newOpp.StageName=DefaultStage; 
		
		system.debug('Teh opp to be created: ' + newOpp);
		Id newOppId;
		try {
			//create new opp
			insert newOpp;
			newOppId=newOpp.id;
		} catch (System.DmlException e) {		
			for (Integer k = 0; k < e.getNumDml(); k++) {
			 System.debug(e.getDmlMessage(k)); 
			}
		}		
		
		//System.debug('newOpp = ' + newOpp.id); 
		return newOppId;
	} 
	
	
}